package co.chuangdian.oflauncher.uc;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;
import cn.onekit.CONFIG;
import cn.onekit.JSON;
import cn.onekit.MESSAGE;
import cn.onekit.android.ACTIVITY;

public class SelectFolderActivity extends OFActivity {
	ListView selectfolder_listview;
	TextView selectfolder_textview;
	JSONArray folders;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.uc_selectfolder);
		Bundle bundle = this.getIntent().getExtras();
		final int current_id = bundle.getInt("current_id");
		final String message_id = bundle.getString("message_id");
		folders = JSON.parses((String) CONFIG.get("folders"));
		//
		selectfolder_listview = (ListView) findViewById(R.id.selectfolder_listview);
		selectfolder_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				JSONObject folder = folders.optJSONObject(position);
				Bundle bundle = new Bundle();
				try {
					int select_id = folder.optInt("id");
					bundle.putInt("folder_id", select_id);
					MESSAGE.send(message_id, null,bundle);
					finish();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
		selectfolder_listview.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return folders.length();
			}

			@Override
			public Object getItem(int position) {
				// TODO 自动生成的方法存根
				return null;
			}

			@Override
			public long getItemId(int position) {
				// TODO 自动生成的方法存根
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ViewCache viewCache;
				if (convertView == null) {
					viewCache = new ViewCache();
					convertView = View.inflate(SelectFolderActivity.this,
							R.layout.uc_selectfolder_row, null);
					viewCache.folder_textview = (TextView) convertView
							.findViewById(R.id.folder_textview);
					viewCache.folder_rediobutton = (RadioButton) convertView
							.findViewById(R.id.folder_rediobutton);
					convertView.setTag(viewCache);

				} else {
					viewCache = (ViewCache) convertView.getTag();
				}
				try {
					JSONObject folder = folders.getJSONObject(position);
					int id = folder.getInt("id");
					viewCache.folder_textview.setText(folder.getString("name"));
					viewCache.folder_rediobutton.setChecked(current_id == id);
				} catch (JSONException e) {

					e.printStackTrace();
				}
				return convertView;
			}

		});

		//
	}

	class ViewCache {
		TextView folder_textview;
		RadioButton folder_rediobutton;
	}
}